ALMaSS  1.0
The Animal, Landscape and Man Simulation System
configurator.h
Go to the documentation of this file.
1 //
2 // configurator.h
3 //
4 /*
5 *
6 *******************************************************************************************************
7 Copyright (c) 2011, Christopher John Topping, University of Aarhus
8 All rights reserved.
9 
10 Redistribution and use in source and binary forms, with or without modification, are permitted provided
11 that the following conditions are met:
12 
13 Redistributions of source code must retain the above copyright notice, this list of conditions and the
14 following disclaimer.
15 Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
16 the following disclaimer in the documentation and/or other materials provided with the distribution.
17 
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
19 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
21 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 ********************************************************************************************************
27 */
28 
29 #ifndef CONFIGURATOR_H
30 #define CONFIGURATOR_H
31 
32 // If defined will compile many methods as inlines for speed.
33 //#define CONF_INLINES
34 
35 #include <stdio.h>
36 #ifdef __UNIX
37 #undef max
38 #endif
39 #include <string>
40 #include <map>
41 #include <vector>
42 //#include <algorithm.h>
43 
44 // This one *has* to be a define!
45 // A global configurator variable will not do.
46 #define CFG_MAX_LINE_LENGTH 512
47 
48 using namespace std;
49 
50 
51 typedef enum {
58 
59 typedef enum {
64 
65 
69 class CfgBase {
70 protected:
71  string m_key;
74 
75  public:
76  CfgBase(const char* a_key, CfgSecureLevel a_level);
77  virtual ~CfgBase(void);
78 
79  const string getkey ( void ) { return m_key; }
80  virtual CfgType gettype ( void ) { return CFG_NONE; }
81  CfgSecureLevel getlevel( void ) { return m_level; }
82 };
83 
87 class CfgInt : public CfgBase
88 {
89 protected:
90  int m_int;
91  int m_max;
92  int m_min;
93 
94  public:
95  CfgInt(const char* a_key, CfgSecureLevel a_level, int a_defval );
96  CfgInt(const char* a_key, CfgSecureLevel a_level, int a_defval, int a_min, int a_max);
97 
98  int value( void ) { return m_int; }
99  virtual void set(int a_newval);
100  virtual CfgType gettype( void ) { return CFG_INT; }
101 };
102 
106 class CfgFloat : public CfgBase
107 {
108 protected:
109  double m_float;
110  double m_min;
111  double m_max;
112 
113 public:
114  CfgFloat(const char* a_key, CfgSecureLevel a_level, double a_defval);
116  CfgFloat(const char* a_key, CfgSecureLevel a_level, double a_defval, double a_min, double a_max);
117 
118  double value(void) { return m_float; }
119  virtual void set(double a_newval);
120  virtual CfgType gettype(void) { return CFG_FLOAT; }
121 };
122 
123 
127 class CfgBool : public CfgBase
128 {
129 protected:
130  bool m_bool;
131 
132  public:
133  CfgBool( const char* a_key, CfgSecureLevel a_level, bool a_defval );
134 
135  bool value( void ) { return m_bool; }
136  void set( bool a_newval ) { m_bool = a_newval; }
137  virtual CfgType gettype( void ) { return CFG_BOOL; }
138 };
139 
140 
144 class CfgStr : public CfgBase
145 {
146 protected:
147  string m_string;
148 
149  public:
150  CfgStr( const char* a_key, CfgSecureLevel a_level, const char* a_defval );
151 
152  const char* value( void ) { return m_string.c_str(); }
153  void set( char* a_newval ) { m_string = a_newval; }
154  virtual CfgType gettype( void ) { return CFG_STRING; }
155 };
156 
157 
162 {
163 protected:
164  map<string, unsigned int> CfgI;
165  vector<CfgBase*> CfgVals;
166 
167  // Private methods and fields for the configuration file parser.
168  unsigned int m_lineno;
169  void ParseCfgLine( char* a_line );
170  void SetCfgInt ( char* a_key, char* a_val );
171  void SetCfgFloat ( char* a_key, char* a_val );
172  void SetCfgBool ( char* a_key, char* a_val );
173  void SetCfgStr ( char* a_key, char* a_val );
174  bool LastDoubleQuote( char* a_rest_of_line );
175 
176  // Discreet security check. Returns true if we terminate line
177  // parsing early.
178  bool SetCfgGatekeeper( const char* a_method,
179  const char* a_key,
180  CfgSecureLevel a_level );
181 
182  // Helper methods.
183  void ShowIdType( unsigned int a_i );
184  char* ExtractString( char* a_line );
185  void DumpSymbols( const char *a_dumpfile,
186  CfgSecureLevel a_level );
187 
188  public:
189  // Write all configuration values with a security level at or below
190  // a_level to a_dumpfile in alphabetical order. a_level must
191  // be CFG_CUSTOM (user settable) or CFG_PUBLIC (user can see
192  // predefined value and the very existence of this parameter).
193  void DumpPublicSymbols( const char *a_dumpfile,
194  CfgSecureLevel a_level );
195 
196  // Dump *all* configuration values, including the private ones,
197  // to a_dumpfile and then calls exit(), noting the event in the error
198  // file!! For debugging purposes only. The call to exit() is there in
199  // order to try and prevent any use of this call from making it into
200  // production code.
201  void DumpAllSymbolsAndExit( const char *a_dumpfile );
202 
203  // Reads and parses a_cfgfile for configuration values. Unknown
204  // or CFG_PRIVATE keys are silently ignored thus preventing snooping
205  // on possibly interesting key values. Returns true if reading and
206  // parsing was error free.
207  bool ReadSymbols( const char *a_cfgfile );
208 
209  // You should never use these ones directly, they are
210  // called automagically by the CfgBase class con/destructor
211  // when needed.
212  Configurator( void );
213  ~Configurator( void );
214 
215  // Please don't use this unless you know what you are doing.
216  // Need to be public as it is used by the CfgBase class and friends.
217  bool Register( CfgBase* a_cfgval, const char* a_key );
218 };
219 
220 extern class Configurator *g_cfg;
221 
222 #endif // CONFIGURATOR_H
223 
224 
225 
Base class for a configurator entry.
Definition: configurator.h:69
virtual CfgType gettype(void)
Definition: configurator.h:80
const string getkey(void)
Definition: configurator.h:79
string m_key
Definition: configurator.h:71
bool m_rangetest
Definition: configurator.h:73
CfgSecureLevel m_level
Definition: configurator.h:72
CfgSecureLevel getlevel(void)
Definition: configurator.h:81
Bool configurator entry class.
Definition: configurator.h:128
virtual CfgType gettype(void)
Definition: configurator.h:137
bool value(void)
Definition: configurator.h:135
bool m_bool
Definition: configurator.h:130
void set(bool a_newval)
Definition: configurator.h:136
Double configurator entry class.
Definition: configurator.h:107
virtual CfgType gettype(void)
Definition: configurator.h:120
double m_float
Definition: configurator.h:109
double m_min
Definition: configurator.h:110
double m_max
Definition: configurator.h:111
double value(void)
Definition: configurator.h:118
Integer configurator entry class.
Definition: configurator.h:88
int m_min
Definition: configurator.h:92
int m_int
Definition: configurator.h:90
virtual CfgType gettype(void)
Definition: configurator.h:100
int m_max
Definition: configurator.h:91
int value(void)
Definition: configurator.h:98
String configurator entry class.
Definition: configurator.h:145
const char * value(void)
Definition: configurator.h:152
void set(char *a_newval)
Definition: configurator.h:153
virtual CfgType gettype(void)
Definition: configurator.h:154
string m_string
Definition: configurator.h:147
A class to provide standard parameter entry facilities.
Definition: configurator.h:162
vector< CfgBase * > CfgVals
Definition: configurator.h:165
unsigned int m_lineno
Definition: configurator.h:168
map< string, unsigned int > CfgI
Definition: configurator.h:164
CfgSecureLevel
Definition: configurator.h:59
@ CFG_PRIVATE
Definition: configurator.h:62
@ CFG_CUSTOM
Definition: configurator.h:60
@ CFG_PUBLIC
Definition: configurator.h:61
CfgType
Definition: configurator.h:51
@ CFG_BOOL
Definition: configurator.h:55
@ CFG_STRING
Definition: configurator.h:56
@ CFG_NONE
Definition: configurator.h:52
@ CFG_INT
Definition: configurator.h:53
@ CFG_FLOAT
Definition: configurator.h:54
class Configurator * g_cfg
Definition: configurator.cpp:49